
//初始化分诊信息显示区域
function initAssignInfoTable(assignInfo){
	if(assignInfo){
		assignInfo.con_start=$.dateFormat(assignInfo.con_start, "yyyy-MM-dd HH:mm")
		assignInfo.con_end=$.dateFormat(assignInfo.con_end, "yyyy-MM-dd HH:mm")
		$('#assignInfo_table').find('span').each(function(index,item){
			var bindname = $(item).attr('bindname');
			if(bindname){
				$(item).html(transItemCode(bindname,assignInfo[bindname]));
			}
		})
	}
}

//加载费用
function initCostInfo(allotDocId){
	$.getApiClient('/cost/selectByDoctor/{docId}').setQuery({
		docId:allotDocId
	}).get().done(function(data){
		if(data.code=='0'){
			if(data.payload){
				$('#cost').html(data.payload.cost);
				$('#manager_cost').html(data.payload.manager_cost);
				
				$('#costTr').show();
				$('#managerCostTr').show();
			}
		}
	});
}

//初始化分诊表单项
function initAssignInfoForm(assignInfo,allotInfo){
	$('#assignInfoForm').validation();
	if(assignInfo && !allotInfo){
		//申请时指定信息有分诊信息无(显示申请时指定信息)
		initAllotHosSel(assignInfo.assign_hid,assignInfo.assign_did,assignInfo.assign_uid);
		
		//初始化会诊开始结束小时下拉框
		initConHourSel(null,null);
		//初始化会诊开始结束分钟下拉框
		initConMinitueSel(null,null);
		
		$('#con_date').val($.dateFormat(new Date(), "yyyy-MM-dd"));
	}else if(!assignInfo && allotInfo){
		//申请时指定信息无分诊信息有（分诊被退回情况）
		initAllotHosSel(allotInfo.allot_hid,allotInfo.allot_did,allotInfo.allot_docid);
		//对会诊日期进行处理
		separateDate(allotInfo);
		$('#assignInfoForm').bindData(allotInfo);
		
		//初始化会诊开始结束小时下拉框
		initConHourSel(allotInfo.start_hour,allotInfo.end_hour);
		//初始化会诊开始结束分钟下拉框
		initConMinitueSel(allotInfo.start_minitue,allotInfo.end_minitue);
	}else{
		//初始化指定医院级拉框
		initAllotHosSel(null,null,null);
		
		//初始化会诊开始结束小时下拉框
		initConHourSel(null,null);
		//初始化会诊开始结束分钟下拉框
		initConMinitueSel(null,null);
		
		$('#con_date').val($.dateFormat(new Date(), "yyyy-MM-dd"));
	}


	//初始化指定医院级拉框
	function initAllotHosSel(defaultHosVal,defaultDeptsVal,defaultUserVal){
		$.getApiClient('/consultation/hospitals/allot').get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
		    multipleSelectData("allot_hid",[{"id": "", "hospital_name": "请选择"}].concat(data.payload), defaultHosVal, {valueAttr:'id', displayAttr:'hospital_name'},'请选择');
			$('#allot_hid').multipleSelect({width:'100%', single: true});
			
			//医院科室下拉框联动
			$('#allot_hid').change(function(){
				var hosid=$(this).children('option:selected').val();
				initAllotDeptSel(hosid,defaultDeptsVal,defaultUserVal);
			}).change();
		});
	}
	
	//初始化指定科室下拉框
	function initAllotDeptSel(hosId,defaultVal,defaultUserVal){
		if(hosId){
			$.getApiClient('/consultation/departments/allot').setQuery({hospital_id:hosId}).get().done(function(data){
				if(!data.payload){
					data.payload=[];
				}
			    multipleSelectData("allot_did",[{"id": "", "department_name": "请选择"}].concat(data.payload), defaultVal, {valueAttr:'id', displayAttr:'department_name'},"请选择");
				$('#allot_did').multipleSelect({width:'100%', single: true});
				
				//科室医生下拉框联动
				$('#allot_did').change(function(){
					var deptId=$(this).children('option:selected').val();
					initAllotDocSel(deptId,defaultUserVal);
				}).change();
			});
		}else{
			multipleSelectData("allot_did",[{"id": "", "department_name": "请选择"}], defaultVal, {valueAttr:'id', displayAttr:'department_name'},"请选择");
			$('#allot_did').multipleSelect({width:'100%', single: true});
			
			//科室医生下拉框联动
			$('#allot_did').change(function(){
				var deptId=$(this).children('option:selected').val();
				initAllotDocSel(deptId,defaultUserVal);
			}).change();
		}
	}
	
	//初始化指定医生下拉框
	function initAllotDocSel(deptId,defaultVal){
		if(deptId){
			$.getApiClient('/consultation/doctors/allot').setQuery({department_id:deptId}).get().done(function(data){
				if(!data.payload){
					data.payload=[];
				}
			    multipleSelectData("allot_docid",[{"id": "", "user_name": "请选择"}].concat(data.payload), defaultVal, {valueAttr:'id', displayAttr:'user_name'},"请选择");
				$('#allot_docid').multipleSelect({width:'100%', single: true});
			});
		}else{
			multipleSelectData("allot_docid",[{"id": "", "user_name": "请选择"}], defaultVal, {valueAttr:'id', displayAttr:'user_name'},"请选择");
			$('#allot_docid').multipleSelect({width:'100%', single: true});
		}
	}
	
	//初始化会诊开始结束小时下拉框
	function initConHourSel(defaultStartHour,defaultEndHour){
		var conHourArry=[];
		for(var i=8;i<=20;i++){
			var hourItem={};
			if(i<10){
				hourItem.code="0"+i;
				hourItem.value="0"+i;
			}else{
				hourItem.code=i;
				hourItem.value=i;
			}
			conHourArry.push(hourItem);
		}
		
		if(defaultStartHour){
			multipleSelectData("start_hour",conHourArry,defaultStartHour);
			$('#start_hour').multipleSelect({width:'20%', single: true});
		}else{
			multipleSelectData("start_hour",conHourArry,"08");
			$('#start_hour').multipleSelect({width:'20%', single: true});
		}

		if(defaultEndHour){
			multipleSelectData("end_hour",conHourArry,defaultEndHour);
			$('#end_hour').multipleSelect({width:'20%', single: true});
		}else{
			multipleSelectData("end_hour",conHourArry,"08");
			$('#end_hour').multipleSelect({width:'20%', single: true});
		}
	}
	
	//初始化会诊开始结束分钟下拉框
	function initConMinitueSel(defaultStartMin,defaultEndMin){
		var conMinitueArry=[{code:'00',value:'00'}];
		for(var i=10;i<60;i=i+10){
			conMinitueArry.push({code:i+'',value:i+''});
		}
		
		if(defaultStartMin){
			multipleSelectData("start_minitue",conMinitueArry,defaultStartMin);
			$('#start_minitue').multipleSelect({width:'22%', single: true});
		}else{
			multipleSelectData("start_minitue",conMinitueArry,"00");
			$('#start_minitue').multipleSelect({width:'22%', single: true});
		}
		
		if(defaultEndMin){
			multipleSelectData("end_minitue",conMinitueArry,defaultEndMin);
			$('#end_minitue').multipleSelect({width:'22%', single: true});
		}else{
			multipleSelectData("end_minitue",conMinitueArry,"00");
			$('#end_minitue').multipleSelect({width:'22%', single: true});
		}
	}
	
	//对会诊日期进行处理
	function separateDate(allotInfo){
		var con_start=new Date(allotInfo.con_start);
		allotInfo['con_date']=$.dateFormat(con_start, "yyyy-MM-dd");
		allotInfo['start_hour']=con_start.getHours()<10 ?'0'+con_start.getHours():con_start.getHours()+'';
		allotInfo['start_minitue']=con_start.getMinutes()<10?'0'+con_start.getMinutes():con_start.getMinutes()+'';
		
		var con_end=new Date(allotInfo.con_end);
		allotInfo['end_hour']=con_end.getHours()<10 ?'0'+con_end.getHours():con_end.getHours()+'';
		allotInfo['end_minitue']=con_end.getMinutes()<10?'0'+con_end.getMinutes():con_end.getMinutes()+'';
	}
}

//收集分诊表单数据
function collectAssignForm(){
	var assignListArr=[];
	var assignFormData=$('#assignInfoForm').collect();
	var conStartStr=$('#con_date').val()+' '+$('#start_hour option:selected').val()+':'+$('#start_minitue option:selected').val()+':00';
	var conEndStr=$('#con_date').val()+' '+$('#end_hour option:selected').val()+':'+$('#end_minitue option:selected').val()+':00';
	$.extend(assignFormData,{
		con_start:$.dateParser(conStartStr, "yyyy-MM-dd HH:mm:ss"),
		con_end:$.dateParser(conEndStr, "yyyy-MM-dd HH:mm:ss"),
		ismaindoc:'1'
	})
	assignListArr.push(assignFormData);
	return {
		assignList:assignListArr
	};
}